package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.action.flow.assign.RegectTaskSingleBackWayCmd;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.entity.organ.LjLs;
import com.hnkywl.sts.entity.organ.search.QueryLj;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.flow.FlowService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.organ.GysService;
import com.hnkywl.sts.service.organ.LjLsService;
import com.hnkywl.sts.service.organ.LjwhService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.jbpm.api.*;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-3-22
 * Time: 上午11:12
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/organ/gys")
public class GysAction extends SimpleActionSupport {
    //页面当前位置
    private int page;
    //每页行数
    private int rows;
    //行索引
    private int index;

    private Yg yg;

    private Lj lj;

    private Long ljId;

    private String syIds;

    private String xyIds;

    private String syJcs;

    private String xyJcs;

    private QueryLj queryLj;

    private String result;

    private String taskId;

    private String content;

    private String processInstanceId;

    private String executionId;

    private FlowService flowService;

    private ProcessEngine processEngine;

    private TaskService taskService;

    private ExecutionService executionService;

    private HistoryService historyService;

    private YgService ygService;

    private GysService gysService;

    private Long jyjgId;

    private Integer lx;

    private LjLs ljLs;

    private Long ljLsId;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private GsjgService gsjgService;
    private LjLsService ljLsService;
    private LjwhService ljwhService;
    private String jyjgjc;
    private String syList;
    private String xyList;

    //添加供应商主页面
    @Action("gys")
    public String gys() {
        yg = this.ygService.findYgDetail(YgService.getCurrentYg().getId());
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //添加供应商主页面列表展示
    @Action("gys_ljwhList")
    public void LjLsList() {
        this.renderJson(this.gysService.findAllLjLs(jyjgId, lx, page, rows));
    }

    //添加供应商详细页面的跳转
    @Action("gys_detail")
    public String LjLsDetail() {
        ljLs = this.gysService.findLjLsDetail(ljLsId);
        return SUCCESS;
    }

    //供应商添加保存
    @Action("gys_lj_save")
    public void save() {
        try {
            this.ljLsService.save(syList, xyList, ljLs);
            renderText("{\"statusCode\":\"200\",\"message\":\"保存成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //添加供应商content页面的跳转
    @Action("gys_content")
    public String gyscontent() {
        try {
            ljLs = new LjLs();
            jyjgjc = gsjgService.findGsjgDetail(jyjgId).getJc();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return SUCCESS;
    }

    //删除修改供应商的记录
    @Action("gys_lj_delete")
    public void delete() {
        try {
            this.gysService.delete(ljLsId);
            renderText("{\"statusCode\":\"200\",\"message\":\"删除成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("ljLsByProcessInstance")
    public void ljLsByProcessInstance() {
        LjLs ljLs;
        String processInstanceId = StringUtils.EMPTY;
        Execution execution = this.executionService.findExecutionById(executionId);
        if (execution != null) {
            ljLs = this.ljLsService.finLjLsDetailByProcessInstance(execution.getProcessInstance().getId());
            processInstanceId = execution.getProcessInstance().getId();
        } else {
            ljLs = this.ljLsService.finLjLsDetailByProcessInstance(executionId);
            processInstanceId = executionId;
        }
        String xyJson = "{'id':'" + ljLs.getId() + "','type':'QYJG','businessNo':'" + ljLs.getId() + "','processInstanceId':'" + processInstanceId + "'}";
        this.renderText(xyJson);
    }

    //提交修改供应商的记录
    @Action("gys_lj_commit")
    public void commitLjLs() {
        try {
            ljLs = this.gysService.findLjLsDetail(ljLsId);
            if (this.flowService.checkFlowStart("供应商流程")) {
                String processInstanceId = this.flowService.regTask(ljLs.getProcessInstanceId(), "供应商流程", StringUtils.EMPTY);
                ljLs.setProcessInstanceId(processInstanceId);
                ljLs.setZt("ytj");
            } else{
                ljLs.setZt("ysp");
                ljLs.setYtb(true);
                ljLsService.transLj(ljLs);
            }
            this.ljLsService.updateLjLs(ljLs);
            renderText("{\"statusCode\":\"200\", \"message\":\"提交供应商记录成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    @Action(value = "examineTask")
    public void examineTask() {
        if (this.result.equals("审核")) {
            Map<String, Object> var = new HashMap<String, Object>();
            var.put("owner", YgService.getCurrentYg().getId());
            var.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
            var.put("result", "审核通过");
            var.put("content", content);
            var.put("time", new Date());
            Task currentTask = this.taskService.getTask(taskId);
            String executionId = currentTask.getExecutionId();
            executionService.createVariables(executionId, var, true);
            var.put("status", result);
            this.taskService.completeTask(taskId, var);

            Execution execution = this.executionService.findExecutionById(executionId);
            if (execution != null) {
                LjLs ljLs = gysService.findLjDetailByProcessInstance(execution.getProcessInstance().getId());
                if (ljLs.getZt().equals("ybc")) {
                    ljLs.setZt("ytj");
                    ljLsService.updateLjLs(ljLs);
                }
            }
        } else {
            String result = processEngine.execute(new RegectTaskSingleBackWayCmd(taskId, content));
            if (result.equals("change")) {
                //修改状态  ytj--->ybc
                String executionId = this.historyService.createHistoryTaskQuery().taskId(taskId).uniqueResult().getExecutionId();
                Execution execution = this.executionService.findExecutionById(executionId);
                LjLs ljLs = gysService.findLjDetailByProcessInstance(execution.getProcessInstance().getId());
                ljLs.setZt("ybc");
                ljLsService.updateLjLs(ljLs);
            }
        }
        this.renderText("{\"statusCode\":\"200\", \"message\":\"审核流程成功\"}");
    }


    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setLjwhService(LjwhService ljwhService) {
        this.ljwhService = ljwhService;
    }

    @Autowired
    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }

    @Autowired
    public void setExecutionService(@Qualifier("executionService") ExecutionService executionService) {
        this.executionService = executionService;
    }

    @Autowired
    public void setTaskService(@Qualifier("taskService") TaskService taskService) {
        this.taskService = taskService;
    }

    @Autowired
    public void setProcessEngine(@Qualifier("processEngine") ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    @Autowired
    public void setHistoryService(@Qualifier("historyService") HistoryService historyService) {
        this.historyService = historyService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getJyjgId() {
        return jyjgId;
    }

    public void setJyjgId(Long jyjgId) {
        this.jyjgId = jyjgId;
    }

    public Integer getLx() {
        return lx;
    }

    public void setLx(Integer lx) {
        this.lx = lx;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Lj getLj() {
        return lj;
    }

    public void setLj(Lj lj) {
        this.lj = lj;
    }

    public Long getLjId() {
        return ljId;
    }

    public void setLjId(Long ljId) {
        this.ljId = ljId;
    }

    public String getSyIds() {
        return syIds;
    }

    public void setSyIds(String syIds) {
        this.syIds = syIds;
    }

    public String getXyIds() {
        return xyIds;
    }

    public void setXyIds(String xyIds) {
        this.xyIds = xyIds;
    }

    public QueryLj getQueryLj() {
        return queryLj;
    }

    public void setQueryLj(QueryLj queryLj) {
        this.queryLj = queryLj;
    }

    public String getProcessInstanceId() {
        return processInstanceId;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getContent() {
        return content;
    }

    public String getSyJcs() {
        return syJcs;
    }

    public void setSyJcs(String syJcs) {
        this.syJcs = syJcs;
    }

    public String getXyJcs() {
        return xyJcs;
    }

    public void setXyJcs(String xyJcs) {
        this.xyJcs = xyJcs;
    }

    public GysService getGysService() {
        return gysService;
    }

    public void setGysService(GysService gysService) {
        this.gysService = gysService;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getExecutionId() {
        return executionId;
    }

    public void setExecutionId(String executionId) {
        this.executionId = executionId;
    }

    public void setProcessInstanceId(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    public LjLs getLjLs() {
        return ljLs;
    }

    public void setLjLs(LjLs ljLs) {
        this.ljLs = ljLs;
    }

    public String getJyjgjc() {
        return jyjgjc;
    }

    public void setJyjgjc(String jyjgjc) {
        this.jyjgjc = jyjgjc;
    }

    @Autowired
    public void setLjLsService(LjLsService ljLsService) {
        this.ljLsService = ljLsService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    public String getSyList() {
        return syList;
    }

    public void setSyList(String syList) {
        this.syList = syList;
    }

    public String getXyList() {
        return xyList;
    }

    public void setXyList(String xyList) {
        this.xyList = xyList;
    }

    public Long getLjLsId() {
        return ljLsId;
    }

    public void setLjLsId(Long ljLsId) {
        this.ljLsId = ljLsId;
    }
}
